﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using University.Workshop.Model;
using System.Collections;
using System.Net.Mime;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;

namespace University.Workshop.DataAccess
{
    public class SendMail : IDisposable
    {
        private SmtpClient smtpMail;
        private MailMessage Message;
        private Attachment data;
        const string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=DataBaseHotel;Integrated Security=True";
        private SqlConnection conex;
        public void SendMessage(MyEmail email)
        {
            if (email == null)
                throw new ArgumentNullException("email");

            try
            {
                Message = new MailMessage(email.To, email.From, email.Subject, email.Message);
                smtpMail = new SmtpClient("smtp.gmail.com");
                Message.IsBodyHtml = false;
                smtpMail.EnableSsl = true;
                smtpMail.UseDefaultCredentials = false;
                smtpMail.Port = 25;
                smtpMail.Credentials = new NetworkCredential(email.To,email.Password);
                if (!string.IsNullOrEmpty(email.File))
                {
                    data = new Attachment(email.File, MediaTypeNames.Application.Octet);
                    // Add time stamp information for the file.
                    ContentDisposition disposition = data.ContentDisposition;
                    disposition.CreationDate = System.IO.File.GetCreationTime(email.File);
                    disposition.ModificationDate = System.IO.File.GetLastWriteTime(email.File);
                    disposition.ReadDate = System.IO.File.GetLastAccessTime(email.File);
                    // Add the file attachment to this e-mail message.
                    Message.Attachments.Add(data);
                }
                smtpMail.Send(Message);
            }
            catch 
            {
                throw new ArgumentException("error in send email");
            }
        }
         public void SelectAdministrator(ListView list)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            conex = new SqlConnection(connectionString);

            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = "SELECT * FROM [DataBaseHotel].[dbo].[Administrator]";
            cmd.CommandType = CommandType.Text;
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            SqlDataReader reader = cmd.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    ListViewItem itemId = new ListViewItem();
                    itemId.SubItems.Add(reader["FullName"].ToString());
                    itemId.SubItems.Add(reader["Email"].ToString());
                    list.Items.Add(itemId);
                }
            }
            catch (Exception e)
            {

                throw new ArgumentException(e.Message);
            }
        }
        public void SelectEmployee(ListView list)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            conex = new SqlConnection(connectionString);

            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = "SELECT * FROM [DataBaseHotel].[dbo].[Employee]";
            cmd.CommandType = CommandType.Text;
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            SqlDataReader reader = cmd.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    ListViewItem itemId = new ListViewItem();
                    itemId.SubItems.Add(reader["Name"].ToString() + reader["Surname"].ToString());
                    itemId.SubItems.Add(reader["Email"].ToString());
                    list.Items.Add(itemId);
                }
            }
            catch (Exception e)
            {

                throw new ArgumentException(e.Message);
            }
        }
        public void SelectClient(ListView list)
        {
            if (list == null)
                throw new ArgumentNullException("list");
            conex = new SqlConnection(connectionString);

            SqlCommand cmd = conex.CreateCommand();
            cmd.CommandText = "SELECT * FROM [DataBaseHotel].[dbo].[Client]";
            cmd.CommandType = CommandType.Text;
            if (cmd.Connection.State == ConnectionState.Closed)
            {
                cmd.Connection.Open();
            }
            SqlDataReader reader = cmd.ExecuteReader();
            try
            {
                while (reader.Read())
                {
                    ListViewItem itemId = new ListViewItem();
                    itemId.SubItems.Add(reader["FullName"].ToString());
                    itemId.SubItems.Add(reader["Email"].ToString());
                    list.Items.Add(itemId);
                }
            }
            catch (Exception e)
            {

                throw new ArgumentException(e.Message);
            }
        }
        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (smtpMail != null || Message != null || conex != null || data != null)
                {
                    smtpMail.Dispose();
                    smtpMail = null;
                    Message.Dispose();
                    Message = null;
                    conex.Dispose();
                    conex = null;
                    data.Dispose();
                    data = null;
                }
            }
        }
        #endregion
    }
}
